'''
Created on 2009-9-14

@author: loudly
'''
from django import newforms as forms
from utils.advancedforms import MultipleCharField
import django.template.loader as loader


class NewGameForm(forms.Form):
    
    name = forms.CharField(max_length = 256)
    
    def __init__(self, numberOfPlayers, *args, **kwargs):
        #NOTE: "base_fields" can and should only be modified before actual init() of the base class
        #      later, only "fields" can be modifed 
        #
        #required should be set to False here, because many games don't have a fixed number
        # of players. In such cases, numberOfPlayers should be set to the max possible value
        # hence some of the sub-fields will be empty
        # in required is set to True, MultiValueField.clean() will raise exception
        self.base_fields['playerArr'] = MultipleCharField(numberOfPlayers, required = False)
        
        super(NewGameForm, self).__init__(*args, **kwargs)

class EmbedView(object):
    '''
    classdocs
    '''
    language = 'en'
    
    gameName = None
    gameTexts = {
            'en' : {  
            }
        }

    ACTION_REDIRECT = 'redirect'
    
    LAYOUT_PLAY_HORIZONTAL = 1

    def __init__(selfparams):
        '''
        Constructor
        '''
    
    @staticmethod
    def _newEmbedReturnValue(content = None, error = None, action = None, target = None, layout = None):
        ret = {}
        
        ret['content'] = content
        ret['error'] = error
        ret['action'] = action
        ret['target'] = target
        ret['layout'] = layout
        
        return ret
    
    def gameText(self, fieldName):
        dict = {}
        
        if self.language == 'blabla' :
            pass
        else :
            dict = self.gameTexts['en']
        
        if fieldName in dict.keys():
            return dict[fieldName]
        else:
            return 'N/A'        
    
    @staticmethod
    def gameListView(game):
        ret = ''
        
        ret += 'GENERAL: '
        ret += game.name + ' '
        ret += str(game.key()) + ' '
        ret += str(game.startTime)
        ret += '<br>\n'
        
        return EmbedView._newEmbedReturnValue(content = ret)
    
    def _newGameInputs(self, newGameForm):
        ret = ''
        
        newGameForm.fields['playerArr'].widget.divAttrs['class'] = 'player_arr'
        newGameForm.fields['playerArr'].widget.inputAttrs['size'] = 12
        
        template_values = {
            'form': newGameForm,
        }
        
        #if newGameForm.errors['playerArr'] :
        #    template_values['playerArrErr'] = ''
        
        ret += loader.render_to_string('newGameForm.html', template_values)
        
        return self._newEmbedReturnValue(content = ret)